// Decompiled by DJ v3.9.9.91 Copyright 2005 Atanas Neshkov  Date: 2008-3-7 16:00:22
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3)
// Source File Name:   DateUtils.java

package com.uwo.house.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

public class DateUtils {

    public DateUtils() {
    }

    public static String formatDate(Date date, String strExpression) {
        SimpleDateFormat sdfDate = new SimpleDateFormat(strExpression);
        return sdfDate.format(date);
    }

    public static String formatDateUs(Date date, String strExpression) {
        SimpleDateFormat sdfDate = new SimpleDateFormat(strExpression, Locale.US);
        return sdfDate.format(date);
    }

    public static String formatDate(Date date) {
        if (date == null) {
            return "";
        } else {
            return formatDate(date, "yyyy-MM-dd");
        }
    }

    public static String getUsDate(Date date, String format) {
        if (date == null) {
            return "";
        } else {
            return formatDateUs(date, format);
        }
    }

    public static Date getDate(String strDate) {
        SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdfDate.parse(strDate);
        } catch (Exception e) {
            date = null;
        }
        return date;
    }

    public static Date getDate(String strDate, String format) {
        SimpleDateFormat sdfDate = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = sdfDate.parse(strDate);
        } catch (Exception e) {
            date = null;
        }
        return date;
    }

    public static String getChineseDate(Date date) {
        return getChineseDate(formatDate(date, "yyyy-MM-dd"));
    }

    public static String getChineseDate(String strDate) {
        if (strDate == null || strDate.length() != 10)
            return "";
        else
            return strDate.substring(0, 4) + "\u5E74" + strDate.substring(5, 7)
                    + "\u6708" + strDate.substring(8, 10) + "\u65E5";
    }

    public static Date getFirstDayOfYear(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(2, 1);
        c.set(5, 1);
        return c.getTime();
    }

    public static Date getLastDayOfYear(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(2, 12);
        c.set(5, 31);
        return c.getTime();
    }

    public static Date getFirstDayOfMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(5, 1);
        return c.getTime();
    }

    public static Date getLastDayOfMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(2, c.get(2) + 1);
        c.set(5, 1);
        c.add(5, -1);
        return c.getTime();
    }

    public static Date getFirstDayOfWeek(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(7, 1);
        return c.getTime();
    }

    public static Date getLastDayOfWeek(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(7, 7);
        return c.getTime();
    }

    /**
     * 得到上一年的1月1号
     *
     * @param
     * @return String
     * @throws Exception
     */
    static public String getPreviousYearStart() {
        return ""
                + (Long.valueOf(getSystemDate(FMT_DATE_YYYY)).longValue() - 1)
                + "-01-01";
    }

    /**
     * 得到上一年的年末
     *
     * @return String
     */
    static public String getPreviousYearEnd() {
        return ""
                + (Long.valueOf(getSystemDate(FMT_DATE_YYYY)).longValue() - 1)
                + "-12-31";
    }

    /**
     * 得到上 N 个月.
     *
     * @param
     * @return String 得到如 200311
     * @author weiliao
     */
    static public String getPreviousAppointMonth(String strThisMonth,
                                                 int lAppointMonth) {
        String strYear = "";
        String strMonth = "";
        long lYear = 0;
        long lMonth = 0;
        String strReturn = "";
        try {
            if (!"".equals(strThisMonth)) {
                // 转换之前的 year 和 month
                strYear = strThisMonth.substring(0, 4);
                strMonth = strThisMonth.substring(4, 6);
                lYear = Long.valueOf(strYear).longValue();
                lMonth = Long.valueOf(strMonth).longValue();
                // 转换中的
                lMonth = lMonth - lAppointMonth;
                while (lMonth <= 0) {
                    lMonth = lMonth + 12;
                    lYear = lYear - 1;
                }
                // 转换后的
                if (lMonth < 10) {
                    strMonth = "0" + lMonth;
                } else {
                    strMonth = String.valueOf(lMonth);
                }
                strReturn = String.valueOf(lYear) + strMonth;
            }
        } catch (Exception ex) {
            System.out.println(ex);
        }
        return strReturn;
    }

    /**
     * 得到上一个月.
     *
     * @param strThisMonth 如 200401
     * @return String 得到如 200312
     * @author weiliao
     */
    static public String getPreviousMonth(String strThisMonth) {
        String strYear = "";
        String strMonth = "";
        String strReturn = "";
        try {
            if (!"".equals(strThisMonth)) {
                strYear = strThisMonth.substring(0, 4);
                strMonth = strThisMonth.substring(4, 6);
                // strReturn = strYear + strMonth;
                if ("01".equals(strMonth)) {
                    strMonth = "12";
                    strYear = String
                            .valueOf(Long.valueOf(strYear).longValue() - 1);
                } else {
                    strMonth = String.valueOf(Long.valueOf(strMonth)
                            .longValue() - 1);
                    if (strMonth.length() < 2) {
                        strMonth = "0" + strMonth;
                    }
                }
            }
            strReturn = strYear + strMonth;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return strReturn;
    }

    /**
     * 得到去年的１２月份.
     *
     * @param strThisMonth 如 200401
     * @return String 得到如 200312
     * @author weiliao
     */
    static public String getPreviousDec(String strThisMonth) {
        return (Long.parseLong(strThisMonth.substring(0, 4)) - 1) + "12";
    }

    /**
     * 得到上一个月.
     *
     * @param strThisMonth formated as YYYY-MM
     * @return String formated as YYYY-MM
     */
    static public String getPreviousMonth1(String strThisMonth) {
        String strYear = "";
        String strMonth = "";
        String strReturn = "";
        try {
            if (!"".equals(strThisMonth)) {
                strYear = strThisMonth.substring(0, 4);
                strMonth = strThisMonth.substring(5, 7);
                if ("01".equals(strMonth)) {
                    strMonth = "12";
                    strYear = String
                            .valueOf(Long.valueOf(strYear).longValue() - 1);
                } else {
                    strMonth = String.valueOf(Long.valueOf(strMonth)
                            .longValue() - 1);
                    if (strMonth.length() < 2) {
                        strMonth = "0" + strMonth;
                    }
                }
            }
            strReturn = strYear + "-" + strMonth;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return strReturn;
    }

    /**
     * 下一个月
     *
     * @param strThisMonth
     * @return String
     */
    static public String getNextMonth(String strThisMonth) {
        String strYear = "";
        String strMonth = "";
        String strReturn = "";
        try {
            if (!"".equals(strThisMonth)) {
                strYear = strThisMonth.substring(0, 4);
                strMonth = strThisMonth.substring(5, 7);
                // strReturn = strYear + strMonth;
                if ("12".equals(strMonth)) {
                    strMonth = "01";
                    strYear = String
                            .valueOf(Long.valueOf(strYear).longValue() + 1);
                } else {
                    strMonth = String.valueOf(Long.valueOf(strMonth)
                            .longValue() + 1);
                    if (strMonth.length() < 2) {
                        strMonth = "0" + strMonth;
                    }
                }
            }
            strReturn = strYear + "-" + strMonth;
            // System.out.println( strYear + strMonth );
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return strReturn;
    }

    /**
     * 得到去年的同一个月.
     *
     * @param strThisMonth 如 200401
     * @return String 得到如 200301
     * @author weiliao
     */
    static public String getLastyearMonth(String strThisMonth) {
        String strYear = "";
        String strMonth = "";
        String strReturn = "";
        try {
            if (!"".equals(strThisMonth)) {
                strYear = strThisMonth.substring(0, 4);
                strMonth = strThisMonth.substring(4, 6);
                strYear = String.valueOf(Long.valueOf(strYear).longValue() - 1);
            }
            strReturn = strYear + strMonth;
            // System.out.println( strYear + strMonth );
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return strReturn;
    }

    /**
     * 计算某日期的前后的日期
     *
     * @param strDate
     * @param days
     * @return
     * @throws ParseException
     */
    public static String addDate(String strDate, int days)
            throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date cDate = df.parse(strDate);
        GregorianCalendar gcalendar = new GregorianCalendar();
        gcalendar.setTime(cDate);
        gcalendar.add(Calendar.DATE, days);
        // System.out.print(df.format(gcalendar.getTime()));
        return df.format(gcalendar.getTime()).toString();
    }

    /**
     * 计算某日期的前后的日期
     *
     * @param date
     * @param days
     * @return
     * @throws ParseException
     */
    public static String addDate(Date date, int days) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        GregorianCalendar gcalendar = new GregorianCalendar();
        gcalendar.setTime(date);
        gcalendar.add(Calendar.DATE, days);
        // System.out.print(df.format(gcalendar.getTime()));
        return df.format(gcalendar.getTime()).toString();
    }

    /**
     * 计算某日期的前后的日期
     *
     * @param date
     * @param days
     * @return
     */
    public static Date addDateAsDate(Date date, int days) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        GregorianCalendar gcalendar = new GregorianCalendar();
        gcalendar.setTime(date);
        gcalendar.add(Calendar.DATE, days);
        // System.out.print(df.format(gcalendar.getTime()));
        return gcalendar.getTime();
    }

    /**
     * 计算某日期的前后的日期
     *
     * @param date
     * @param days
     * @return
     * @throws ParseException
     */
    public static Date addDateWithFormat(Date date, int days) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        GregorianCalendar gcalendar = new GregorianCalendar();
        gcalendar.setTime(date);
        gcalendar.add(Calendar.DATE, days);
        return df.parse(df.format(gcalendar.getTime()));
    }

    /**
     * 计算某日期的前后的时间
     *
     * @param date
     * @param days
     * @return
     * @throws ParseException
     */
    public static String addDatetime(Date date, int days) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        GregorianCalendar gcalendar = new GregorianCalendar();
        gcalendar.setTime(date);
        gcalendar.add(Calendar.DATE, days);
        // System.out.print(df.format(gcalendar.getTime()));
        return df.format(gcalendar.getTime()).toString();
    }

    /**
     * 计算某日期的前后的日期
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public static String addmonth(Date date) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
        GregorianCalendar gcalendar = new GregorianCalendar();
        gcalendar.setTime(date);
        gcalendar.add(Calendar.DATE, 0);
        // System.out.print(df.format(gcalendar.getTime()));
        return df.format(gcalendar.getTime()).toString();
    }

    public static boolean checkTime(String d, String h) {
        String[] a = h.split(":");
        long hh = Long.parseLong(a[0]);
        String m = a[1];
        String time = "";
        if (hh > 4) {
            hh = hh - 5;
            time = d + " " + hh + ":" + m + ":00";
        } else {
            hh = hh + 19;
            // 得到前一天
            time = getPreviousDate(d) + " " + hh + ":" + m + ":00";
        }
        System.out.println(time);
        return isDateBefore(time);
    }

    /**
     * 得到前一天
     *
     * @param strDate 日期format as YYYY-MM-DD
     */
    static public String getPreviousDate(String strDate) {
        String strReturn = "";
        if (strDate != null && !strDate.equals("")) {
            String strYear = strDate.substring(0, 4);
            String strMonth = strDate.substring(5, 7);
            String strDay = strDate.substring(8, 10);
            java.util.Calendar calendar = Calendar.getInstance();
            calendar.set(Integer.parseInt(strYear, 10), Integer.parseInt(
                    strMonth, 10) - 1, Integer.parseInt(strDay, 10) - 1);
            strReturn = new SimpleDateFormat("yyyy-MM-dd").format(calendar
                    .getTime());
        }
        return strReturn;
    }

    // 判断当前时间是否在时间date2之前
    // 时间格式 2005-4-21 16:16:34
    public static boolean isDateBefore(String date2) {
        try {
            if (date2.length() < 11) {
                date2 = date2 + " 00:00:00";
            }
            Date date1 = new Date();
            DateFormat df = DateFormat.getDateTimeInstance();
            return date1.before(df.parse(date2));
        } catch (ParseException e) {
            System.out.print("[SYS] " + e.getMessage());
            return false;
        }
    }

    public static boolean isDateBefore(String date1, String date2) {
        try {
            if (date1.length() < 11) {
                date1 = date1 + " 00:00:00";
            }
            if (date2.length() < 11) {
                date2 = date2 + " 00:00:00";
            }
            DateFormat df = DateFormat.getDateTimeInstance();
            return df.parse(date1).before(df.parse(date2));
        } catch (ParseException e) {
            System.out.print("[SYS] " + e.getMessage());
            return false;
        }
    }

    public static Date addTime(Date date, int year, int month, int day, int week) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(1, year);
        c.add(2, month);
        c.add(5, day);
        c.add(3, week);
        return c.getTime();
    }

    public static String nextMonthFirstDay(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-d");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(c.MONTH, 1);
        c.set(c.DATE, 1);
        return sdf.format(c.getTime());
    }

    public static String backMonthFirstDay(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(c.MONTH, -1);
        // c.set(c.DATE, 1);
        return sdf.format(c.getTime());
    }

    public static Date getSystemDate() {
        Calendar c = Calendar.getInstance();
        return c.getTime();
    }

    public static String getSystemDate(String strExpression) {
        return formatDate(getSystemDate(), strExpression);
    }

    public static String getSystemTime(String strTime) {
        return getSystemDate(strTime);
    }

    public static int getDateCompare(Date startDate, Date endDate) {
        int compareValue = -2;
        try {
            double diffMilliSeconds = endDate.getTime() - startDate.getTime();
            if (diffMilliSeconds > 0) {
                compareValue = 1;
            } else if (diffMilliSeconds == 0) {
                compareValue = 0;
            } else if (diffMilliSeconds < 0) {
                compareValue = -1;
            }
        } catch (Exception ex) {
        }

        return compareValue;
    }

    // startDate,endDate格式为2009-01
    public static String[] get2DateBetweenMonths(String startDate,
                                                 String endDate) {
        String startDates[] = startDate.split("-");
        String endDates[] = endDate.split("-");
        int yearStart = Integer.parseInt(startDates[0]);
        int monthStart = Integer.parseInt(startDates[1]);
        int yearEnd = Integer.parseInt(endDates[0]);
        int monthEnd = Integer.parseInt(endDates[1]);
        int months = (yearEnd - yearStart) * 12 + (monthEnd - monthStart);
        if (months == 0) {
            return new String[]{startDate};
        }
        if (months < 0) {
            months = -months;
            int tempYear = yearStart;
            yearStart = yearEnd;
            yearEnd = tempYear;
            int tempMonth = monthStart;
            monthStart = monthEnd;
            monthEnd = tempMonth;
        }
        String[] dates = new String[months + 1];
        int n = 0;
        int m = 0;
        for (int i = yearStart * 12 + monthStart; i <= yearEnd * 12 + monthEnd; i++) {
            m = i % 12;
            if (m == 0) {
                dates[n] = ((i / 12) - 1) + "-" + 12;
            } else {
                if (i % 12 < 10) {
                    dates[n] = (i / 12) + "-0" + (m);
                } else {
                    dates[n] = (i / 12) + "-" + (m);
                }
            }
            n++;

        }

        return dates;

    }

    // 取出月份数组
    public static String[] getMonsByDate(String dateStr) throws ParseException {

        String[] lookDates = new String[12];
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sf2 = new SimpleDateFormat("MM");
        Date d = sf.parse(dateStr);
        for (int i = -11; i <= 0; i++) {
            GregorianCalendar gc = new GregorianCalendar();
            gc.setTime(d);
            gc.add(2, i);
            gc.set(gc.get(Calendar.YEAR), gc.get(Calendar.MONTH), gc
                    .get(Calendar.DATE));
            lookDates[11 + i] = sf2.format(gc.getTime());
        }

        return lookDates;
    }

    public static String getFirstDayOfWeek() {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.set(Calendar.DAY_OF_WEEK_IN_MONTH, c.getFirstDayOfWeek());
        return sf.format(c.getTime());
    }

    public static String changeToWeek(String week) {

        String strWeek = "";
        switch (Integer.parseInt(week)) {
            case 1:
                strWeek = "一";
                break;
            case 2:
                strWeek = "二";
                break;
            case 3:
                strWeek = "三";
                break;
            case 4:
                strWeek = "四";
                break;
            case 5:
                strWeek = "五";
                break;
            case 6:
                strWeek = "六";
                break;
            case 7:
                strWeek = "日";
                break;

        }
        return strWeek;
    }

    public static Date addMinute(Date strDate, int minutes)
            throws ParseException {
        GregorianCalendar gcalendar = new GregorianCalendar();
        gcalendar.setTime(strDate);
        gcalendar.add(Calendar.MINUTE, minutes);
        // System.out.print(df.format(gcalendar.getTime()));
        return gcalendar.getTime();
    }

    public static Date addDatetimeAsDate(Date date, int days) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        GregorianCalendar gcalendar = new GregorianCalendar();
        gcalendar.setTime(date);
        gcalendar.add(Calendar.DATE, days);
        // System.out.print(df.format(gcalendar.getTime()));
        return gcalendar.getTime();
    }

    public static boolean isToday(Date date) {
        SimpleDateFormat fmt = new SimpleDateFormat(FMT_DATE_YYYY_MM_DD);
        if (fmt.format(date).toString().equals(fmt.format(new Date()).toString())) {
            return true;
        } else {
            return false;
        }
    }

    public static final String FMT_DATE_YYYY = "yyyy";

    public static final String FMT_DATE_YYMMDD = "yyMMdd";

    public static final String FMT_DATE_YYYYMMDD = "yyyyMMdd";

    public static final String FMT_DATE_YYYY_MM_DD = "yyyy-MM-dd";

    public static final String FMT_DATE_DATETIME = "yyyy-MM-dd HH:mm:ss";

    public static final String FMT_DATE_DATETIME2 = "yyyy-MM-dd HH:mm";

    public static final String FMT_DATE_DATETIME_TIGHT = "yyyyMMddHHmmss";

    public static final String FMT_DATE_YY_MM = "yy-MM";

    public static final String FMT_DATE_MM = "MM";

    public static final String FMT_DATE_DD = "dd";

    public static final String FMT_DATE_TIME = "HH:mm:ss";

}